package apimanager;

import android.os.NetworkOnMainThreadException;

import entity.ImageSharingApiBaseInfo;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.Request;

public interface RequestMessage {

    default void requestMessage(okhttp3.Callback callback, String apiPath, String requestMode, RequestBody body) {
        ImageSharingApiBaseInfo apiBaseInfo = ImageSharingApiBaseInfo.getInstance();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Request request = new Request.Builder()
                        .url(apiBaseInfo.getAPP_LOCATION()+apiPath)
                        .method(requestMode, body)
                        .addHeader("Content-Type", "application/json")
                        .addHeader(apiBaseInfo.getAPP_ID_KEY(), apiBaseInfo.getAPP_ID())
                        .addHeader(apiBaseInfo.getAPP_SECRET_KEY(), apiBaseInfo.getAPP_SECRET())
                        .build();
                try {
                    OkHttpClient client = new OkHttpClient();
                    client.newCall(request).enqueue(callback);
                } catch (NetworkOnMainThreadException ex) {
                    ex.printStackTrace();
                }
            }
        }).start();
    }
}

